﻿using System;
using System.Web;

namespace NS12.Com.Tracking
{
    public class TrackingFilterer : MikeKappel.Com.CS.HttpModules.StreamFilter.IFilterer
    {
        #region Properties
        // <summary>
        /// 
        /// </summary>
        public static String ImageFile
        {
            get
            {
                return "/spacer.gif";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String ImageTracking
        {
            get
            {
                return String.Format("\n<img src=\"{0}\" />\n", ImageFile);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String JavaScriptTracking
        {
            get
            {
                return "<script type=\"text/javascript\">" +
                    "\nfunction pulser(){" +
                    "\n var xmlhttp = new XMLHttpRequest();" +
                    "\n xmlhttp.onreadystatechange = function() {" +
                    "\n     if (xmlhttp.readyState==4 && xmlhttp.status==200) {" +
                    "\n         var pulseTo = setTimeout(function(){pulser();},5000);" +
                    "\n         eval(xmlhttp.responseText);" +
                    "\n     }" +
                    "\n }" +
                    "\n xmlhttp.open(\"GET\", \"/" + ((HttpContext.Current.Session != null) ? TrackingModule.VisitorID.ToString() : "Pulse") + "/\", true);" +
                    "\n xmlhttp.send(null);" +
                    "\n}" +
                    "\npulser();" +
                    "\n</script>\n";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public String JavaScriptTrackingPost
        {
            get
            {
                return "<script type=\"text/javascript\">" +
                    "\nfunction pulser(){" +
                    "\n var params = \"vid=" + TrackingModule.VisitorID.ToString() + "\";" +
                    "\n var xmlhttp = new XMLHttpRequest();" +
                    "\n xmlhttp.onreadystatechange = function() {" +
                    "\n     if (xmlhttp.readyState==4 && xmlhttp.status==200) {" +
                    "\n         var pulseTo = setTimeout(function(){pulser();},5000);" +
                    "\n         eval(xmlhttp.responseText);" +
                    "\n     }" +
                    "\n }" +
                    "\n xmlhttp.open(\"POST\", \"Pulse\");" +
                    "\n xmlhttp.setRequestHeader(\"Content-Type\", \"text/plain;charset=UTF-8\");" +
                    "\n xmlhttp.setRequestHeader(\"Content-length\", params.length);" +
                    "\n xmlhttp.setRequestHeader(\"Connection\", \"close\");" +
                    "\n xmlhttp.send(params);" +
                    "\n}" +
                    "\npulser();" +
                    "\n</script>\n";
            }
        }
        #endregion
        #region Constructors
        public TrackingFilterer()
        {
        }
        #endregion
        #region Methods
        public String Filter(String unfiltered) {
            String filtered = unfiltered;

            if (!filtered.Contains(ImageTracking))
                filtered = filtered.Replace("</body>", ImageTracking + "</body>");

            if (!filtered.Contains(JavaScriptTracking))
                filtered = filtered.Replace("</body>", JavaScriptTracking + "</body>");

            return filtered;
        }
        #endregion
    }
}
